package com.example.community.event;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.community.entity.Event;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

/**
 * @Author: huang
 * @Date: 2021/10/4 11:47
 * @Description:
 */
@Component
public class EventProducer {

    @Autowired
    private KafkaTemplate kafkaTemplate;

    //处理事件
    public void fireEvent(Event event) {
        //将事件发布到指定的主题
        //event.getTopic() 得到topic；JSONObject.toJSONString(event)) 将event转化成json字符串直接作为context
        kafkaTemplate.send(event.getTopic(), JSONObject.toJSONString(event));
    }
}
